package com.ztsoft.user.service.dynamic.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.dynamic.FormDetailMapper;
import com.ztsoft.user.pojo.entity.dynamic.*;
import com.ztsoft.user.pojo.vo.dynamic.DynamicFormVo;
import com.ztsoft.user.pojo.vo.dynamic.SearchVerifyVo;
import com.ztsoft.user.service.dynamic.FormDataService;
import com.ztsoft.user.service.dynamic.FormDetailService;
import com.ztsoft.user.service.dynamic.SearchVerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FormDetailServiceImpl extends ServiceImpl<FormDetailMapper, FormDetail> implements FormDetailService {
    @Autowired
    private FormDataService formDataService;
    @Autowired
    private SearchVerifyService searchVerifyService;

    @Override
    public void saveFormDetail(FormDetail formDetail) {
        Date date = new Date(System.currentTimeMillis());
        formDetail.setCreateTime(date);
        formDetail.setModifyTime(date);
        baseMapper.insert(formDetail);
        String id = formDetail.getId();

        List<String> formDataList = formDetail.getFormDataList();
        for (String value : formDataList) {
            FormData formData = new FormData(id, value);
            formData.setCreateTime(date);
            formData.setModifyTime(date);
            formDataService.save(formData);
        }

        List<SearchVerify> verifyList = formDetail.getVerifyList();
        for (SearchVerify searchVerify : verifyList) {
            searchVerify.setFormDetailId(id);
            searchVerify.setCreateTime(date);
            searchVerify.setModifyTime(date);
            searchVerifyService.save(searchVerify);
        }
    }

    @Override
    public List<FormDetail> queryAllFormDetail(FormDetail formDetail) {
        return list(new QueryWrapper<FormDetail>(formDetail).orderBy(true, true, "sequence"));
    }

    //表单明细相关
    @Override
    public void generateFormDetail(List<DynamicFormVo> formList,
                                   DynamicForm dynamicForm,
                                   Map<String, List<SearchVerifyVo>> rules,
                                   Map<String, Object> defaultValueMap) {
        List<FormDetail> formDetailList = this.list(new QueryWrapper<FormDetail>().eq("dynamic_form_id", dynamicForm.getId()).orderBy(true, true, "sequence"));
        for (FormDetail formDetail : formDetailList) {
            DynamicFormVo dynamicFormVo = new DynamicFormVo();
            dynamicFormVo.setLabel(formDetail.getLabel());
            dynamicFormVo.setModel(formDetail.getModel());
            dynamicFormVo.setType(formDetail.getType());

            List<FormData> formDataList = formDataService.list(new QueryWrapper<FormData>().eq("form_detail_id", formDetail.getId()));
            switch (formDetail.getType()) {
                case "input":
                    dynamicFormVo.setPlaceholder(formDetail.getPlaceholder());
                    break;
                case "radio":
                    List<String> itemRadioList = new ArrayList<>();
                    for (FormData formData : formDataList) {
                        itemRadioList.add(formData.getValueStr());
                    }
                    dynamicFormVo.setItemRadioList(itemRadioList);
                    break;
                case "select":
                    List<Option> options = new ArrayList<>();
                    for (FormData formData : formDataList) {
                        Option option = new Option(formData.getValueStr(), formData.getValueStr());
                        options.add(option);
                    }
                    dynamicFormVo.setOptions(options);
                    break;
                default:
                    break;
            }
            List<SearchVerifyVo> verifyList = searchVerifyService.querySearchVerifyVo(formDetail.getId());
            if (!verifyList.isEmpty()) {
                rules.put(formDetail.getModel(), verifyList);
            }
            if (formDetail.getDefaultValue() != null) {
                defaultValueMap.put(formDetail.getModel(), formDetail.getDefaultValue());
            }
            formList.add(dynamicFormVo);
        }
    }
}
